<template>
  <div class="goodsinfo">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag"></div>
    </transition>
    <!-- 轮播区域 -->
    <div class="panel">
      <swipe :lunbotuList="lunbotuList" class="swiper"></swipe>
    </div>

    <!-- 列表信息 -->
    <van-panel class="panel" :title="goodsinfo.title">
      <div class="panel-body">
        <div class="price-group">
          市场价格：
          <span class="old">{{goodsinfo.market_price}}</span>
          销售价格：
          <span class="new">{{goodsinfo.sell_price}}</span>
        </div>
        <div class="stepper">
          <div class="number">购买数量:</div>
          <!--
            :min="1" 默认就是1 不添加也可以
            plus 点击添加的时候触发
            minus 点击减少的时候触发
          -->
          <van-stepper v-model="value" integer class="number"/>
        </div>
        <div class="btn-group">
          <van-button type="primary" size="small">立即购买</van-button>
          <van-button type="danger" size="small" @click="addToShopCart" :disabled="btnFlag">加入购物车</van-button>
        </div>
      </div>
    </van-panel>
    <!-- 列表信息 -->
    <van-panel class="panel" title="商品信息">
      <div class="panel-body">
        <p>商品货号：{{ goodsinfo.goods_no }}</p>
        <p>库存情况：{{ goodsinfo.stock_quantity }}件</p>
        <p>上架时间：{{ goodsinfo.add_time | datefmt }}</p>
      </div>
    </van-panel>
    <div>
      <p>1</p>
      <p>2</p>
      <p>3</p>
      <p>4</p>
      <p>5</p>
      <p>6</p>
      <p>7</p>
      <p>8</p>
      <p>9</p>
      <p>10</p>
      <p>11</p>
      <p>12</p>
    </div>
  </div>
</template>
<script>
import swipe from "../../components/swipe";
export default {
  data() {
    return {
      value: 0,
      lunbotuList: [],
      id: this.$route.params.id,
      goodsinfo: {},
      ballFlag: false,
      XDist:0,
      YDist:0,
      btnFlag:false, //添加购物车节流阀
    };
  },
  created() {
    this.getthumimages();
    this.getgoodsinfo();
  },
  methods: {
    /* 
      轮播区域
      */
    async getthumimages() {
      const {
        data: { message, status }
      } = await this.$http.get("api/getthumimages/" + this.id);
      if (status == 0) {
        this.lunbotuList = message;
      } else {
        Toast("获取失败");
      }
    },

    /* 
    获取商品信息
    */
    async getgoodsinfo() {
      const {
        data: { message, status }
      } = await this.$http.get("api/goods/getinfo/" + this.id);
      if (status == 0) {
        this.goodsinfo = message;
      } else {
        Toast("获取失败");
      }
    },
    /* 
    小球半场动画
    */
    getBount(){
        let ball = document.querySelector(".ball").getBoundingClientRect();
        let info = document.querySelector(".van-info").getBoundingClientRect();

        //移动的距离
        this.XDist = info.left - ball.left;
        this.YDist =  info.y - ball.y;   
    },
    beforeEnter(el) {
      //设置元素的起始位置
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      // 强制刷新
      el.offsetTop;
      // 获取移动距离
      this.getBount();
      //设置元素的目的地
      el.style.transform = `translate(${this.XDist}px,${this.YDist}px)`;
      //过渡时间
      el.style.transtion = "all 0.5s ease";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag; // 取反表示动画执行完让小球隐藏
    },
    /*
    加入购物车
    */
    addToShopCart() {
      this.ballFlag = !this.ballFlag; // 点击后 让小球显示
      this.btnFlag = !this.btnFlag; //点击后取反 表示禁用
      // 当动画执行完之后取消禁用
      setTimeout(() => {
          this.btnFlag = !this.btnFlag;
      }, 500);

      //先包装要添加的数据对象

      var goodsinfo = {
        id:this.id,
        count:this.value,
        sell_price:this.goodsinfo.sell_price,
        selected:true,
        title:this.goodsinfo.title,
        src:this.lunbotuList[0].src,
        timer:null
      }
      // console.log(goodsinfo);
      
      // 调用mutations里面的 函数更新 state
      this.$store.commit('addToCart',goodsinfo)
      
    }
  },
  components: {
    swipe
  }
};
</script>
<style lang="less">
.goodsinfo {
  position: relative;
  .ball {
    width: 15px;
    height: 15px;
    background: red;
    border-radius: 50%;
    position: absolute;
    left: 130px;
    top: 300px;
    // top: 360px;
    z-index: 520;
  }
  .panel {
    width: 96%;
    border-radius: 6px;
    box-shadow: 0 0 3px #ccc;
    margin: 0 auto;
    margin-top: 10px;
    overflow: hidden;
    .swiper {
      text-align: center;
      height: 166px;
      line-height: 166px;
      img {
        width: 100%;
        vertical-align: middle;
      }
    }
    .panel-body {
      margin-left: 16px;
      margin-bottom: 10px;
      font-size: 14px;
      .price-group {
        margin-bottom: 12px;
        .old {
          text-decoration: line-through;
          margin-right: 6px;
          color: #8f8f94;
        }
        .new {
          color: red;
          font-size: 18px;
        }
      }
      .stepper {
        color: #8f8f94;
        font-size: 0;
        .number {
          display: inline-block;
          font-size: 14px;
          margin-right: 6px;
        }
      }
      .btn-group {
        margin-top: 10px;
      }
      p {
        margin-top: 10px;
      }
    }
  }
}
</style>